package com.eco.fanliapp.job.orderjobNew;


import com.eco.fanliapp.common.OrderTaskUtils;
import com.eco.fanliapp.entity.TaobaoScheme;
import com.eco.fanliapp.service.IJobOrderInfoHandleNewService;
import com.eco.fanliapp.utils.DateUtils;
import org.apache.commons.collections.CollectionUtils;
import org.quartz.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.Serializable;
import java.util.List;

/**
 * 失效订单
 */
@DisallowConcurrentExecution
@PersistJobDataAfterExecution
public class OrderFailOneMonthTaskNew implements Job, Serializable {

    private static final Logger log = LoggerFactory.getLogger(OrderFailOneMonthTaskNew.class);

    @Autowired
    private IJobOrderInfoHandleNewService jobOrderInfoHandleNewService;

    @Autowired
    private OrderTaskUtils orderTaskUtils;


    public OrderFailOneMonthTaskNew() {
    }

    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {

        //取得job详情
        JobDetail jobDetail = context.getJobDetail();

        JobDataMap dataMap =  jobDetail.getJobDataMap();

        int executeCount = 0;
        if (dataMap.containsKey("runCount")) {
            executeCount = dataMap.getInt("runCount");
        }

        // 增量计数并将其存储回 JobDataMap，这样可以适当保持工作状态
        executeCount++;
        dataMap.put("runCount", executeCount);

        long beginTime = System.currentTimeMillis();
        log.info("***************************************开始执行定时任务-拉取15天内失效订单定时任务-OrderFailOneMonthTaskNew：executeCount=" + executeCount + "--beginTime=" + beginTime);

        try {
            String timeFlag ="";

            //获取淘宝联盟账号-排除已过期的
            List<TaobaoScheme> taobaoSchemes = orderTaskUtils.getUsedTaobaoSchemeList();

            //切分时间
            List<String> list = jobOrderInfoHandleNewService.handleTime(timeFlag, 60);

            String startTime = "";
            if(CollectionUtils.isNotEmpty(list)){
                for (String time : list) {
                    if (startTime == null || "".equals(startTime)) {
                        startTime = time;
                        continue;
                    }
                    String endTime = time;
                    Long dealEndTime = DateUtils.datestr2LongByFormt(endTime, null);
                    if (dealEndTime >= (System.currentTimeMillis() - 1000L * 60 * 60 *24 * 14)) {
                        continue;
                    }
                    //处理订单
                    jobOrderInfoHandleNewService.handleFailOrder(startTime, endTime, taobaoSchemes, 0);
                    startTime = time;
                }

            }

        } catch (Exception e) {
            e.printStackTrace();
            log.error("定时任务错误-拉取失效订单定时任务-OrderFailTask：executeCount=" + executeCount);
        }finally {
            long endTime = System.currentTimeMillis();
            log.info("***************************************结束执行定时任务-拉取15天内失效订单定时任务-OrderFailOneMonthTaskNew：executeCount=" + executeCount + "--endTime=" + endTime + "--耗时=" + (endTime - beginTime));
        }
    }

}
